/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 11951
 * Date: 2025-11-03
 * Time: 14:27
 */
public class Demo {
    //1.查找目标元素(顺序查找)
    static int findKey(int[] array, int key) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key){
                return i;
            }
        }
        return -1;
    }

//2.二分查找
    static int binarySearch(int[] arry,int key){
        int left = 0;
        int right = arry.length-1;
        while (left<=right){
            int mid = (left+right)/2;//中间值
            if(key<arry[mid]){
                //目标值在中间值左侧
                right=mid-1;//直接去左侧找
            } else if (key>arry[mid]) {
                //目标值在中间值右侧
                left=mid+1;//去右侧找
            }else{
                //找到了
                return mid;
            }
        }
        return -1;

    }
//3.冒泡排序
    static void bubbleSort(int[]array){
        for (int i = 0; i < array.length; i++) {//遍历数组
            for (int j = 1; j < array.length-i; j++) {
                if(array[j-1]>array[j]){
                    int tmp =array[j-1];
                    array[j-1]=array[j];
                    array[j]=tmp;

                }
            }
        }
    }
}
